using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

// WORKFLOW: 背包内格子的类型
public enum SlotType
{
    BAG, ACTION, WEAPON, SHIELD, HELMET, ARMOR, BOOTS
}
/// <summary>
/// 背包格子
/// </summary>
public class SlotUI : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
{
    public ItemUI itemUI;
    public SlotType slotType;

    public void OnPointerClick(PointerEventData eventData)
    {
        // 双击
        if (eventData.clickCount % 2 == 0)
        {
            UseItem();
        }
    }
    /// <summary>
    /// 使用消耗品
    /// </summary>
    public void UseItem()
    {
        // 双击空的背包格子时不执行任何操作
        if (itemUI.GetItem() == null)
        {
            return;
        }
        if (itemUI.GetItem().itemType == ItemType.USEABLE &&
        itemUI.Bag.inventoryList[itemUI.Index].inventoryAmount > 0)
        {
            // WORKFLOW: 消耗品的使用效果：回血
            GameManager.Instance.playerStat.ApplyHealth(itemUI.GetItem().useableItemData.healthPoint);
            // 背包中消耗品数量减一
            itemUI.Bag.inventoryList[itemUI.Index].inventoryAmount -= 1;
            // 更新任务
            QuestManager.Instance.UpdateQuestProcess(itemUI.GetItem().name, -1);
            // 某些消耗品可能影响最大生命值或攻击
            // InventoryManager.Instance.UpdateHPAndAtk(GameManager.Instance.playerStat.MaxHealth, GameManager.Instance.playerStat.attackData.minDamage, GameManager.Instance.playerStat.attackData.maxDamage);
        }
        UpdateItemUI();
    }
    /// <summary>
    /// 更新格子的UI
    /// </summary>
    public void UpdateItemUI()
    {
        // WORKFLOW: 背包内部格子物品对应的数据库实例
        switch (slotType)
        {
            case SlotType.BAG:
                itemUI.Bag = InventoryManager.Instance.inventoryData;
                break;
            case SlotType.ACTION:
                itemUI.Bag = InventoryManager.Instance.actionData;
                break;
            case SlotType.WEAPON:
                itemUI.Bag = InventoryManager.Instance.equipmentData;
                // 切换武器
                if (itemUI.GetItem() != null && itemUI.GetItem().itemType == ItemType.WEAPON)
                {
                    GameManager.Instance.playerStat.ChangeWeapon(itemUI.GetItem());
                }
                // 武器耐久度没了就卸载武器
                else
                {
                    GameManager.Instance.playerStat.UnEquipWeapon();
                }
                break;
            case SlotType.SHIELD:
                itemUI.Bag = InventoryManager.Instance.equipmentData;
                break;
            case SlotType.HELMET:
                itemUI.Bag = InventoryManager.Instance.equipmentData;
                break;
            case SlotType.BOOTS:
                itemUI.Bag = InventoryManager.Instance.equipmentData;
                break;
            case SlotType.ARMOR:
                itemUI.Bag = InventoryManager.Instance.equipmentData;
                // TODO: 切换盔甲
                break;
        }
        if (itemUI.Bag != null)
        {
            // 从背包数据库中拿到对应序号的物品
            InventoryItem item = itemUI.Bag.inventoryList[itemUI.Index];
            // 更新对应的UI
            itemUI.Setup(item.itemData, item.inventoryAmount);
        }
        else
        {
            Debug.Log(transform.parent.parent.name + "/" + transform.parent.name + "/" + gameObject.name);
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (itemUI.GetItem())
        {
            InventoryManager.Instance.toolTip.SetupToolTop(itemUI.GetItem());
            if (itemUI.GetItem().itemType==ItemType.WEAPON)
            {
                InventoryManager.Instance.toolTip.AddWeaponInfo(itemUI.GetItem().weaponData);
            }
            InventoryManager.Instance.toolTip.gameObject.SetActive(true);
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        InventoryManager.Instance.toolTip.gameObject.SetActive(false);
    }
    private void OnDisable()
    {
        InventoryManager.Instance.toolTip.gameObject.SetActive(false);
    }
}
